<?php
namespace app\beautiful\controller\admin\appointment;
use app\BaseController;
use app\beautiful\model\appointment\AppointmentModel;
use app\common\logic\DataLogic;
use app\common\model\ShopStoreModel;


class Kanban extends BaseController{
    public function show()
    {
        $day=$this->request->param('day');
        $store_id = $this->request->param('store_id/d',0);
        if(empty($day)) return $this->error('参数错误');
        if(false === strtotime($day.' 00:00:00')) return $this->error('参数错误');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['day','=',$day]
        ];
       
        if($store_id > 0) {
            $where[]=['store_id','=',$store_id];
        }
        $appointment_model= new AppointmentModel();
        $total=$appointment_model->where($where)->group('technician_id')->count();
        $appointment_model = $appointment_model->with(['technician']);
        $order=['count'=>'desc'];
        $field="technician_id,count(*) as count";
        $list=$appointment_model
            ->where($where)
            ->field($field)
            ->group('technician_id')
            ->order($order)
            ->select()
            ->toArray();
        
        $technician_ids=array_column($list,'technician_id');
        $app = [
            ['shop_id','=',$this->request->shopId],
            ['day','=',$day],
            ['technician_id','in',$technician_ids],
        ];
        if($store_id > 0) {
            $app[] = ['store_id','=',$store_id];
        }
        $appointment_data=AppointmentModel::with(['technician','member'])->where($app)->select()->append(['status_means','operate_type_means','arrival_time_format','cancel_time_format','add_time_format'])->toArray();
        $technician_data=[];
        foreach($appointment_data as $k=>$v){
            $technician_data[$v['technician_id']][]=$v;
        }

        foreach ($list as $k => $v){
            $list[$k]['orders']=$technician_data[$v['technician_id']];
        }
        
        $where_store = [
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['enable','=',1],
        ];
        $field='*';
        $order=['is_main'=>'asc','add_time'=>'desc'];
        $store = ShopStoreModel::where($where_store)->field($field)->order($order)->select()->toArray();
        $return=[
            'total'=>$total,
            'list'=>$list,
            'store' => $store,
        ];
        return $this->success($return);
    }
}
